package com.kpx.app.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.AddressBean;
import com.kpx.app.bean.TaskBean;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.MyWebView;
import com.kpx.app.wigdet.DialogFactoryImpl;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/3/3.
 * 任务详情
 */

public class ActivityTaskDetail extends ActivityBase {
    public static int intype;
    public static final int DATING_TYPE_IN = 0;
    public static final int YUGAO_TYPE_IN = 1;
    @BindView(R.id.webview_task)
    MyWebView webviewTask;
    @BindView(R.id.txt_back)
    TextView txtBack;
    @BindView(R.id.txt_todo)
    TextView txtTodo;
    private TaskBean taskBean;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_task_detail);
        ButterKnife.bind(this);
        UIUtils.setNotifyColor(ActivityTaskDetail.this, R.color.transparent);
        UIUtils.initWebView(ActivityTaskDetail.this, webviewTask);
    }

    @Override
    public void initData() {
        taskBean = (TaskBean) getIntent().getExtras().getSerializable(Constants.KEY_TASK_BEAN);
        if (taskBean != null && taskBean.getTaskGoods() != null && !TextUtils.isEmpty(taskBean.getTaskGoods().getGoodsUrl())) {
            if (UserInfoUtils.isUserLogin()) {
                webviewTask.loadUrl(taskBean.getDetailUrl() + "&userId=" + UserInfoUtils.getCachedLoginUser().getUSER().getDmId());
            } else {
                webviewTask.loadUrl(taskBean.getDetailUrl());
            }
        }
        refreshData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (intype == DATING_TYPE_IN) {
            getTaskById();
        }
    }

    /**
     * 设置相关数据
     */
    private void refreshData() {
        if (taskBean != null && taskBean.getTaskGoods() != null && !TextUtils.isEmpty(taskBean.getTaskGoods().getGoodsUrl())) {
            if (UserInfoUtils.isUserLogin() && UserInfoUtils.getCachedLoginUser().getUSER().getSteps() <= 2) {
                txtTodo.setEnabled(true);
                txtTodo.setBackgroundColor(getResources().getColor(R.color.red_FF4088));
                txtTodo.setText("照片认证之后才能进行抢单哦");
            } else {
                txtTodo.setEnabled(taskBean.getIsSnatchStatus() == 1);
                txtTodo.setBackgroundColor(getResources().getColor(taskBean.getIsSnatchStatus() == 1 ? R.color.red_FF4088 : R.color.gray_c8));
                txtTodo.setText(taskBean.getIsSnatchStatusText());
            }
        }
    }

    @Override
    public void onReceiver(Context context, Intent intent) {
        if (intent.getAction().equals(BroadcastFilters.BROADCAST_ACTION_REFRESH_TASK_DATA)) {
//            getTaskById();
        }
    }

    private void getTaskById() {
        if (taskBean == null) return;
        MyHashMap map = new MyHashMap();
        map.put("taskInfoId", taskBean.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        taskBean = (TaskBean) result.getData();
                        refreshData();
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityTaskDetail.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ToastUtil.getInstance().showToast(ActivityTaskDetail.this, error);
            }

        }, TaskBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_GET_TASK_BY_ID, true, map);
    }

    @OnClick({R.id.txt_back, R.id.txt_todo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_back://返回
                finish();
                break;
            case R.id.txt_todo://相关操作－－抢单－状态显示
                if (!UserInfoUtils.isUserLogin()) {
                    UserInfoUtils.showLoginDialog(this, "登录之后才能进行抢单操作", 1);
                    return;
                }
                if (UserInfoUtils.isUserLogin() && UserInfoUtils.getCachedLoginUser().getUSER().getSteps() <= 2) {
                    DialogFactoryImpl.newInstance(this).showConfirmDialog("我再看看", "现在就去", "温馨提示", "照片认证之后才能进行抢单哦", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_NEGATIVE:
                                    dialog.dismiss();
                                    break;
                                case DialogInterface.BUTTON_POSITIVE:
                                    IntentUtil.gotoActivity(ActivityTaskDetail.this, ActivityCertification.class);
                                    dialog.dismiss();
                                    break;
                            }
                        }
                    }, true);
                    return;
                }
                if (taskBean == null) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.KEY_TASK_BEAN, taskBean);
                IntentUtil.gotoActivity(ActivityTaskDetail.this, ActivityOrderSure.class, bundle);
                break;
        }
    }
}
